package com.transmit.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {

    private static SimpleDateFormat yearMonthDayFormat = new SimpleDateFormat("yyyy-MM-dd");

    public static String dateFormat(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static String dateFormat(Date date) {
        return yearMonthDayFormat.format(date);
    }

    public static Date strToDateByFormat(String str, String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(str);
    }

    public static Date strToDateByCST(String str) {
        try {
            String format = "EEE MMM dd HH:mm:ss zzz yyyy";
            SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
            return sdf.parse(str);
        } catch (Exception e) {
            return null;
        }
    }

    public static Date strToDateByFormat(String str) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.parse(str);
        } catch (Exception e) {
            return null;
        }
    }

    public static Date strToTime(String str) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.parse(str);
        } catch (Exception e) {
            return null;
        }
    }

    public static String transENStrToCNStr(String dateStr, String regex) {
        String[] dateItems = dateStr.split(regex);
        String[] units = {"年", "月", "日"};
        StringBuilder sb = new StringBuilder();
        for (int i = 0, j = dateItems.length; i < j; i++) {
            sb.append(dateItems[i]).append(units[i]);
        }
        return sb.toString();
    }

    public static long getDaysBetweenDates(String startDateStr, String endDateStr, String formate)
            throws ParseException {
        Date startDate = strToDateByFormat(startDateStr, formate);
        Date endDate = strToDateByFormat(endDateStr, formate);
        Calendar startCal = Calendar.getInstance();
        startCal.setTime(startDate);
        Calendar endCal = Calendar.getInstance();
        endCal.setTime(endDate);
        return (endCal.getTimeInMillis() - startCal.getTimeInMillis()) / (1000L * 3600L * 24L);
    }

    public static Date javaDate2MySqlDate(Date javaDate) {
        if (javaDate == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(javaDate);
        calendar.add(Calendar.HOUR_OF_DAY, -8);
        return calendar.getTime();
    }

    public static Date getExpireTime(Integer minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MINUTE, minutes);
        return calendar.getTime();
    }

    //本月第一天
    public static String getMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        //1:本月第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return DateUtil.dateFormat(calendar.getTime());
    }

    //本月最后一天
    public static String getMonthLastDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return DateUtil.dateFormat(calendar.getTime());

    }

    //获取上个月的最后一天
    public static String getLastMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return DateUtil.dateFormat(calendar.getTime());
    }

    //获取上个月的最后一天
    public static String getLastMonthLastDay() {
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return DateUtil.dateFormat(calendar.getTime());

    }

    //下一年
    public static String getNextYear(String str) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sdf.parse(str));
        calendar.add(Calendar.YEAR, +1);
        return sdf.format(calendar.getTime());
    }
}
